/*
 * Created by Jaren at 2021/10/29 15:45
 */
import 'dart:typed_data';

import 'package:binary/binary.dart';
import 'package:inmotion_protocol/ezcan/bean/settings.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

abstract class ScooterSettings extends AbsSettings {
  static const int speedUnitKmph = 0; // 车辆速度单位，Km/h
  static const int speedUnitMph = 1; // 车辆速度单位，mph

  static const int lightOff = 0; // 前灯关
  static const int lightOn = 0xFF; // 前灯开

  Uint4 cruiseState = Uint4.zero; // 定速巡航开关

  Uint4 lockState = Uint4.zero; // 锁车

  Uint16 limitSpeed = Uint16.zero; // 限速

  ScooterSettings.fromBytes(Uint8ClampedList slowInfoRspMsgExtraBytes) {
    if (slowInfoRspMsgExtraBytes.length > 56) cruiseState = ByteUtil.toUint4(slowInfoRspMsgExtraBytes[56]);
    if (slowInfoRspMsgExtraBytes.length > 60) {
      limitSpeed = ByteUtil.toUint16(ByteUtil.bytes2Uint32Le(slowInfoRspMsgExtraBytes, 60).value * 100); // EzCAN滑板系列限度单位为1km/h
    }
  }
}

class L8Settings extends ScooterSettings {
  static final List _stylesAllowedDiyLightEffectColor = [1, 2, 3, 4, 5];

  static const int lightEffectTailLightFlag = 12; // 尾灯标识，获取尾部氛围灯颜色和获取前部氛围灯颜色为同一个接口，使用不同的flag标识

  Uint4 lightEffectState = Uint4.zero; // 氛围灯开关
  Uint4 lightEffectId = Uint4.zero; // 氛围灯样式

  Uint32 frontLightEffectColor = Uint32.zero; // 前部氛围灯颜色
  Uint32 tailLightEffectColor = Uint32.zero; // 尾部轮毂灯颜色

  L8Settings.fromBytes(Uint8ClampedList slowInfoRspMsgExtraBytes, [int offset = 0]) : super.fromBytes(slowInfoRspMsgExtraBytes) {
    cruiseState = ByteUtil.bytes2Uint4Le(slowInfoRspMsgExtraBytes, 56);
  }

  static bool canDiyLightEffectColor(int id) {
    return _stylesAllowedDiyLightEffectColor.contains(id);
  }

  @override
  String toString() {
    var frontLightEffectColorRgb = ByteUtil.num2Bytes(frontLightEffectColor.value, 3);
    var tailLightEffectColorRgb = ByteUtil.num2Bytes(tailLightEffectColor.value, 3);

    return 'L8Settings{cruiseState: ${cruiseState.value}, '
        'lockState: ${lockState.value}, limitSpeed: ${limitSpeed.value}, lightEffectState: ${lightEffectState.value}, '
        'lightEffectId: ${lightEffectId.value}, frontLightEffectColor: $frontLightEffectColorRgb, '
        'tailLightEffectColor: $tailLightEffectColorRgb}';
  }

  static int parseLightEffectColor(Uint8ClampedList bytes) {
    // bytes[0]为车灯数量，在L8中无意义，忽略

    int color = 0x0;
    color <<= 8;
    color |= bytes[1] & 0xFF; // R
    color <<= 8;
    color |= bytes[2] & 0xFF; // G
    color <<= 8;
    color |= bytes[3] & 0xFF; // B

    return color;
  }
}
